<?php

/**
 * Message
 * 
 * Message controller delivers all required actions (methods) for
 * handling messages.
 * 
 * @author Oliver Schwarz <oliver.schwarz@gmail.com>
 * @package Controller
 */
class Message
{

        private $oCtrl, $aUser;
        public function __construct()
        {
                $this->oCtrl = Util::parseRequest();
                $this->aUser = Auth::check();
        }

        /**
         * Get messages
         * 
         * The main method to retrieve messages from the message
         * table.
         * 
         * @param int $limit Amount of messages
         * @param int $offset Offset for paging elements/messages
         * @return array Array of messages
         */ 
        public function get($offset = 0)
        {

                $limit = MESSAGES_PER_PAGE;
                $offset = $offset * MESSAGES_PER_PAGE;
                $sql = sprintf("
                        SELECT
                                m.id,
                                m.message,
                                FROM_UNIXTIME(m.created) AS timestamp,
                                m.created AS unix_timestamp,
                                u.firstname AS name_first,
                                CONCAT(u.firstname, ' ', SUBSTRING(u.lastname, 1, 1), '.') AS name_short,
                                CONCAT(u.firstname, ' ', u.lastname) AS name_full,
                                CONCAT(u.lastname, ', ', u.firstname) AS name_formal
                        FROM
                                messages m,
                                users u
                        WHERE
                                m.author = u.id
                        ORDER BY
                                created DESC LIMIT %d, %d",
                        $offset,
                        $limit);
                $res = DB::get($sql);
                $tagurl = APP_URL . '/tag/';
                foreach ($res as $item):
                        $result[$item['id']] = array(
                                'id' => $item['id'],
                                'message' => $item['message'],
                                'message_display' => preg_replace('/#([aA-zZ0-9-]*)/', "<a href=\"$tagurl$1\">$1</a>", $item['message']),
                                'date_long' => $item['timestamp'],
                                'date_from' => Util::calculateDateFrom($item['unix_timestamp']),
                                'name_first' => $item['name_first'],
                                'name_short' => $item['name_short'],
                                'name_full' => $item['name_full'],
                                'name_formal' => $item['name_formal']);
                endforeach;
                return $result;

        }
        public function create()
        {

                if ($this->aUser === false):
                        die('You\'re not allowed to do so');
                endif;
                $message = Util::sanitize($_POST['message']);
                preg_match_all("/#[aA-zZ0-9-]*/", $message, $tags);
                $userid = $this->aUser['id'];
                $sql = sprintf("
                        INSERT INTO %s (message, author, created) VALUES ('%s', %s, UNIX_TIMESTAMP())",
                        'messages',
                        $message,
                        $userid);
                DB::getInstance()->query($sql);
                if (isset($tags[0]) && count($tags[0]) > 0):
                        Util::debug($tags);
                        foreach ($tags[0] as $item):
                                $tag = substr($item, 1);
                                $sql = sprintf("
                                        INSERT INTO %s (tag, quantity) VALUES ('%s', 1) ON DUPLICATE KEY UPDATE quantity = quantity + 1",
                                        'tags',
                                        $tag);
                                DB::getInstance()->query($sql);
                        endforeach;
                endif;

                Util::go('/');

        }
        public function run()
        {

                switch($this->oCtrl->action):
                        case 'create':
                                $this->create();
                        break;
                endswitch;

        }

}




?>